import { Vue, Component, Prop, Provide, Emit, Watch } from 'vue-property-decorator';
import { CreateElement } from 'vue';
import { Subject, Subscription } from 'rxjs';
import { ControlInterface } from '@/interface/control';
import { UICounter } from '@/utils';
import './dr2-drtab.less';




@Component({
    components: {
         
    }
})
export default class DR2 extends Vue implements ControlInterface {

    /**
     * 名称
     *
     * @type {string}
     * @memberof DR2
     */
    @Prop() public name?: string;

    /**
     * 视图通讯对象
     *
     * @type {Subject<ViewState>}
     * @memberof DR2
     */
    @Prop() public viewState!: Subject<ViewState>;

    /**
     * 视图状态事件
     *
     * @protected
     * @type {(Subscription | undefined)}
     * @memberof DR2
     */
    protected viewStateEvent: Subscription | undefined;
    


    /**
     * 序列号
     *
     * @private
     * @type {number}
     * @memberof DR2
     */
    private serialNumber: number = this.$util.createSerialNumber();

    /**
     * 请求行为序列号数组
     *
     * @private
     * @type {any[]}
     * @memberof DR2
     */
    private serialsNumber: any[] = [];

    /**
     * 添加序列号
     *
     * @private
     * @param {*} action
     * @param {number} serialnumber
     * @memberof DR2
     */
    private addSerialNumber(action: any, serialnumber: number): void {
        const index = this.serialsNumber.findIndex((serial: any) => Object.is(serial.action, action));
        if (index === -1) {
            this.serialsNumber.push({ action: action, serialnumber: serialnumber })
        } else {
            this.serialsNumber[index].serialnumber = serialnumber;
        }
    }

    /**
     * 删除序列号
     *
     * @private
     * @param {*} action
     * @returns {number}
     * @memberof DR2
     */
    private getSerialNumber(action: any): number {
        const index = this.serialsNumber.findIndex((serial: any) => Object.is(serial.action, action));
        return this.serialsNumber[index].serialnumber;
    }

    /**
     * 关闭视图
     *
     * @param {any[]} args
     * @memberof DR2
     */
    public closeView(args: any[]): void {
        let _this: any = this;
        _this.$emit('closeview', args);
    }


    /**
     * 获取多项数据
     *
     * @returns {any[]}
     * @memberof DR2
     */
    public getDatas(): any[] {
        return this.items;
    }

    /**
     * 获取单项树
     *
     * @returns {*}
     * @memberof DR2
     */
    public getData(): any {
        return this.selection;
    }

    /**
     * 数据选中项
     *
     * @type {*}
     * @memberof DR2
     */
    public selection: any = {};

    /**
     * 父数据
     *
     * @private
     * @type {*}
     * @memberof DR2
     */
    private parentData: any = {};

    /**
     * 关系栏数据项
     *
     * @type {any[]}
     * @memberof DR2
     */
    public items: any[] = [
        {
            index: 0, 
            id: 'form',
            name: 'form', 
            text: '用户组', 
            disabled: false, 
        },
        {
            index: 1, 
            id: 'dritem1',
            name: 'dritem1', 
            text: '用户组成员', 
            disabled: false, 
        },
    ];

    /**
     * 生命周期
     *
     * @memberof DR2
     */
    public created(): void {
        if (this.viewState) {
            this.viewStateEvent = this.viewState.subscribe(({ tag, action, data }) => {
                if (!Object.is(tag, this.name)) {
                    return;
                }
                if (Object.is('state', action)) {
                    if (data.srfkey && !Object.is(data.srfkey, '')) {
                        Object.assign(this.parentData, { srfparentkey: data.srfkey });
                    }
                    const state = !this.parentData.srfparentkey || (this.parentData.srfparentkey && Object.is(this.parentData.srfparentkey, '')) ? true : false;
                    this.setItemDisabled(state);
                }
            });
        }
        this.$nextTick(() => {
            this.$emit('selectionchange', [this.items[0]]);
        });
    }

    /**
     * vue 生命周期
     *
     * @memberof DR2
     */
    public destroyed() {
        if (this.viewStateEvent) {
            this.viewStateEvent.unsubscribe();
        }
    }

    /**
     * 获取关系项
     *
     * @private
     * @param {*} [arg={}]
     * @returns {*}
     * @memberof DR2
     */
    private getDRTabItem(arg: any = {}): any {
        let expmode = arg.nodetype.toUpperCase();
        if (!expmode) {
            expmode = '';
        }
        if (Object.is(expmode, 'DRITEM1')) {
            return {  
                viewname: 'user-group-detail-grid-view', 
                parentdatajo: {},
			};
        }
        return undefined;
    }

    /**
     * 设置关系项状态
     *
     * @param {boolean} state
     * @memberof DR2
     */
    public setItemDisabled(state: boolean): void {
        this.items.forEach((item: any) => {
            if (Object.is(item.name, 'form')) {
                item.disabled = false;
            } else {
                item.disabled = state;
            }
        });
    }

    /**
     * 获取数据项
     *
     * @private
     * @param {string} id
     * @returns {*}
     * @memberof DR2
     */
    private getItem(id: string): any {
        const arr: any[] = this.items.filter((_item: any) => Object.is(_item.id, id));
        if (arr) {
            return arr[0];
        }
        return null;
    }

    /**
     * 选中节点
     *
     * @param {*} $event
     * @memberof DR2
     */
    public tabPanelClick($event: any): void {
        const item = this.getItem($event);
        if (Object.is(item.id, this.selection.id)) {
            return;
        }
        this.$emit('selectionchange', [item]);

        const refview = this.getDRTabItem({ nodetype: item.id });
        this.selection = {};
        const params: any = { ...JSON.parse(JSON.stringify(this.parentData)) };
        if (refview && refview.parentdatajo) {
            Object.assign(params, refview.parentdatajo);
            Object.assign(this.selection, { view: { viewname: refview.viewname }, data: params });
        }
        Object.assign(this.selection, item);
    }

    /**
     * 绘制 用户组 分页
     *
     * @param {string} id
     * @returns
     * @memberof DR2
     */
    public render_form(id: string) {
        const arr: any[] = this.items.filter((_item: any) => Object.is(_item.id, id));
        const [item] = arr;
        return (
            <tab-pane index={item.index} name={item.name} tab={this.name} class='' disabled={item.disabled}
                label={
                    (h: any) => {
                        return (
                            <span class='caption'>
                                {item.text}
                            </span>
                        );
                    }
                }>
                <div class='main-data'>
                    {this.$slots.default}
                </div>
            </tab-pane>
        );
    }

    /**
     * 绘制 用户组成员 分页
     *
     * @param {string} id
     * @returns
     * @memberof DR2
     */
    public render_dritem1(id: string) {
        const arr: any[] = this.items.filter((_item: any) => Object.is(_item.id, id));
        const [item] = arr;
        return (
            <tab-pane index={item.index} name={item.name} tab={this.name} class='' disabled={item.disabled}
                label={
                    (h: any) => {
                        return (
                            <span class='caption'>
                                {item.text}
                            </span>
                        );
                    }
                }>
                {
                    (
                        Object.is(this.selection.id, 'dritem1') &&
                        this.selection.view &&
                        !Object.is(this.selection.view.viewname, '')
                    ) ?
                        this.$createElement(this.selection.view.viewname, {
                            class: {
                                viewcontainer2: true,
                            },
                            props: {
                                viewdata: JSON.stringify({ srfparentdata: this.selection.data }),
                            },
                            key: this.$util.createUUID(),
                        })
                        : ''
                }
            </tab-pane>
        );
    }

    /**
     * 绘制内容
     *
     * @memberof DR2
     */
    public render() {
        return (
            <tabs animated={false} class='app-dr-tab' name={this.name} on-on-click={($event: any) => this.tabPanelClick($event)}>
                {this.render_form('form')}
                {this.render_dritem1('dritem1')}
            </tabs>
        );
    }

}
